package com.franklin.plugins.common.utils;

import com.franklin.plugins.common.collection.PrettyArrayList;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author Franklin
 * @since 2021/8/12 18:24
 */
public class StringHelper {

    public static boolean isGetSetMethod(String methodName) {
        return startWith(methodName, "get") || startWith(methodName, "set");
    }

    /**
     * 以什么开头，但不等于什么
     *
     * @param target
     * @param prefix
     * @return
     */
    public static boolean startWith(String target, String prefix) {
        if (DataCheckUtils.isEmpty(target) || DataCheckUtils.isEmpty(prefix)) {
            return false;
        }
        return target.startsWith(prefix) && !target.equals(prefix);
    }

    public static String whitespace(int length) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            stringBuilder.append(' ');
        }
        return stringBuilder.toString();
    }

    public static boolean isCamel(String str) {
        if (str.contains("_")) {
            return false;
        }
        return Character.isLowerCase(str.charAt(0));
    }

    public static String underlineToCamel(String param) {
        if (DataCheckUtils.isEmpty(param)) {
            return "";
        }
        if (isCamel(param)) {
            return param;
        }
        String temp = param.toLowerCase();
        int len = temp.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = temp.charAt(i);
            if (c == '_') {
                if (++i < len) {
                    sb.append(Character.toUpperCase(temp.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 字符串驼峰转下划线格式
     *
     * @param param 需要转换的字符串
     * @return 转换好的字符串
     */
    public static String camelToUnderline(String param) {
        if (DataCheckUtils.isEmpty(param)) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c) && i > 0) {
                sb.append('_');
            }
            sb.append(Character.toLowerCase(c));
        }
        return sb.toString();
    }

    /**
     * 最长公子串<br>
     *
     * @param str1:
     * @param str2:
     * @return java.lang.String
     * @author Franklin
     * @since 2021/8/12 18:24
     */
    public static String longestCommonSubString(String str1, String str2) {
        if (DataCheckUtils.isEmpty(str1) || DataCheckUtils.isEmpty(str2)) {
            return "";
        }
        int[][] subResults = new int[str1.length() + 1][str2.length() + 1];
        int maxLen = 0;
        int lastIndex = -1;
        for (int i = 1; i <= str1.length(); i++) {
            for (int j = 1; j <= str2.length(); j++) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    subResults[i][j] = subResults[i - 1][j - 1] + 1;
                    if (subResults[i][j] > maxLen) {
                        maxLen = subResults[i][j];
                        lastIndex = i;
                    }
                } else {
                    subResults[i][j] = 0;
                }
            }
        }
        return str1.substring(lastIndex - maxLen, lastIndex);
    }

    /**
     * 首字母小写<br>
     *
     * @param param:
     * @return java.lang.String
     * @author Franklin
     * @since 2021/8/13 14:38
     */
    public static String firstToLowerCase(String param) {
        if (DataCheckUtils.isEmpty(param)) {
            return "";
        }
        return param.substring(0, 1).toLowerCase() + param.substring(1);
    }

    public static String firstToUpperCase(String param) {
        if (DataCheckUtils.isEmpty(param)) {
            return "";
        }
        return param.substring(0, 1).toUpperCase() + param.substring(1);
    }

    /**
     * 去除文件名后缀<br>
     *
     * @param fileName:
     * @return java.lang.String
     * @author Franklin
     * @since 2021/8/13 16:20
     */
    public static String removeFileSuffix(String fileName) {
        if (DataCheckUtils.isEmpty(fileName)) {
            return "";
        }
        int dotIndex = fileName.indexOf(".");
        if (dotIndex < 1) {
            return "";
        }
        return fileName.substring(0, dotIndex);
    }

    public static boolean equals(String str1, String str2) {
        if (DataCheckUtils.isEmpty(str1)) {
            return DataCheckUtils.isEmpty(str2);
        }
        if (DataCheckUtils.isEmpty(str2)) {
            return false;
        }
        return str1.equals(str2);
    }

    public static String getSimpleName(String qualifiedName) {
        if (DataCheckUtils.isEmpty(qualifiedName)) {
            return "";
        }
        if (!qualifiedName.contains(".")) {
            return qualifiedName;
        }
        String[] names = qualifiedName.split("\\.");
        return names[names.length - 1];
    }

    /**
     * 表名转实体类名<br>
     *
     * @param tableName:
     * @return java.lang.String
     * @author Franklin
     * @since 2021/9/2 11:23
     */
    public static String getEntityClassName(String tableName) {
        return firstToUpperCase(underlineToCamel(tableName));
    }

    /**
     * 实体类名转表名<br>
     *
     * @param entityName:
     * @return java.lang.String
     * @author Franklin
     * @since 2021/9/9 18:37
     */
    public static String getTableName(String entityName) {
        return firstToLowerCase(camelToUnderline(entityName));
    }

    public static String fillElExpression(String target, String elExpression, String fillValue) {
        String tempTarget = target;
        int index;
        while ((index = tempTarget.indexOf(elExpression)) >= 0) {
            tempTarget = tempTarget.substring(0, index) + fillValue + target.substring(index + elExpression.length());
        }
        return tempTarget;
    }

    /**
     * El表达式填值<br>
     *
     * @param target:
     * @param fillValue:
     * @return java.lang.String
     * @author Franklin
     * @since 2021/9/22 17:10
     */
    public static String fillElExpressionAll(String target, String fillValue) {
        StringBuilder resultBuilder = new StringBuilder();
        for (int i = 0; i < target.length(); i++) {
            char c = target.charAt(i);
            if (c == '#' || c == '$') {
                int j = i + 1;
                boolean isEl = true;
                while (j < target.length() && target.charAt(j) != '{') {
                    if (target.charAt(j) == ' ') {
                        j++;
                    } else {
                        isEl = false;
                        break;
                    }
                }
                if (isEl) {
                    while (j < target.length() && target.charAt(j) != '}') {
                        j++;
                    }
                    i = j;
                    resultBuilder.append(" ").append(fillValue).append(" ");
                }
            } else {
                resultBuilder.append(c);
            }
        }
        return resultBuilder.toString();
    }

    /**
     * 寻找El表达式<br>
     *
     * @param target:
     * @return java.lang.String
     * @author Franklin
     * @since 2021/9/22 17:10
     */
    public static List<String>  findElExpression(String target) {
        List<String> result = new PrettyArrayList<>();
        for (int i = 0; i < target.length(); i++) {
            char c = target.charAt(i);
            if (c == '#' || c == '$') {
                int j = i + 1;
                boolean isEl = true;
                while (j < target.length() && target.charAt(j) != '{') {
                    if (target.charAt(j) == ' ') {
                        j++;
                    } else {
                        isEl = false;
                        break;
                    }
                }
                if (isEl) {
                    while (j < target.length() && target.charAt(j) != '}') {
                        j++;
                    }
                    result.add(StringUtils.deleteWhitespace(target.substring(i, j + 1)));
                    i = j + 1;
                }
            }
        }
        return result;
    }

}
